<template>
    <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import * as echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './resize'
import { sevenTotalFlow } from "@/api/site/isRecord";
const animationDuration = 6000

export default {
    mixins: [resize],
    props: {
        className: {
            type: String,
            default: 'chart'
        },
        width: {
            type: String,
            default: '100%'
        },
        height: {
            type: String,
            default: '250px'
        },
        chartData: {
            type: Array,
            // required: true
        },
        valueTimes: {
            type: Array,
            default: () => []
        }
    },
    data() {
        return {
            chart: null,
            chartList: []
        }
    },
    watch: {
        valueTimes: {
            deep: true,
            handler(val) {
                this.indexTotalFlowFun(val)
            }
        }
    },
    mounted() {
        this.$nextTick(() => {
            this.initChart()
        })
    },
    beforeDestroy() {
        if (!this.chart) {
            return
        }
        this.chart.dispose()
        this.chart = null
    },
    created() {
        this.indexTotalFlowFun([])
    },
    methods: {
        indexTotalFlowFun(val) {
            console.log(val, 111111111)
            if (val.length > 0) {
                sevenTotalFlow(this.addDateRange({}, val)).then((res) => {
                    this.chartList = res.data
                    this.initChart()
                })
            } else {
                sevenTotalFlow({}).then((res) => {
                    console.log(res)
                    this.chartList = res.data
                    this.initChart()
                })
            }
        },
        initChart() {
            this.chart = echarts.init(this.$el, 'macarons')
            // this.setOptions(this.chartData)
            this.setOptions()
        },
        setOptions(numData) {
            this.chart.setOption({
                tooltip: {
                    trigger: 'axis',
                    axisPointer: { // 坐标轴指示器，坐标轴触发有效
                        type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                    }
                },
                grid: {
                    top: '10%',
                    left: '3%',
                    right: '4%',
                    bottom: '3%',
                    containLabel: true
                },
                xAxis: [{
                    type: 'category',
                    boundaryGap: false,
                    data: this.chartList.map(res => res.weekday),
                    axisTick: {
                        alignWithLabel: true
                    }
                }],
                yAxis: [{
                    type: 'value',
                    axisTick: {
                        show: false
                    }
                }],
                series: [{
                    name: '',
                    type: 'line',
                    // stack: 'vistors',
                    areaStyle: {},
                    data: this.chartList.map(res => res.log_value),
                    // data: [11, 22, 33, 44, 55, 66, 77],
                    animationDuration,
                    itemStyle: {
                        color: {
                            type: 'linear',
                            x: 0,
                            y: 0,
                            x2: 0,
                            y2: 1,
                            colorStops: [
                                { offset: 0, color: 'rgba(6, 165, 77, 1)' }, // 0% 处的颜色
                                { offset: 1, color: 'rgba(49, 245, 137, 1)' } // 100% 处的颜色
                            ],
                            global: false // 缺省为 false
                        },
                    },
                }],

            })
        }
    }
}
</script>
